package org.micode.demo.web.controller;

import org.micode.common.annotation.NoNeedLogin;
import org.micode.common.domain.ApiResult;
import org.micode.common.domain.vo.DictVO;
import org.micode.common.helper.I18nMsgHelper;
import org.micode.demo.biz.service.BizLog4jService;
import org.micode.demo.web.base.BaseWebAdminController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/web-admin/log4j")
public class Log4jController extends BaseWebAdminController {

    @Autowired
    private BizLog4jService bizLog4jService;

    @NoNeedLogin
    @PostMapping("/changeLogLevel")
    public ApiResult<Boolean> changeLogLevel(
            @RequestBody @Validated DictVO<String, String> logDict) {
        log.debug(I18nMsgHelper.get("api.call.log4j.changeLogLevel") + ": {}", logDict);
        String clientIp = getLoginClientIp();
        Boolean ret = bizLog4jService.resetLogLevel(logDict.getCode(), logDict.getValue(), clientIp);
        return doReturnSuccess(ret);
    }

}
